package com.itfreer.data.service.check;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.data.check.entity.CheckHistoryEntity;
import com.itfreer.data.check.entity.CheckInfo;
import com.itfreer.data.check.service.CheckService;
import com.itfreer.power.entity.user.VUserInfoEntity;
import com.itfreer.utils.json.JsonUtils;

/**
 * 检查结果服务
 * 
 * @author cai
 * @param <T>
 *
 */
@Component
@Path("/checkservice/")
public class RestBaseCheckService {

	@Autowired
	private CheckService checkService;

	private final static String s_user = "s_user";

	@POST
	@Path("/check")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public List<CheckHistoryEntity> check(@Context HttpServletRequest request, String data) throws IOException {
		HashMap<String, Object> args = getRestParam(data);
		String entityJson = null;
		String name = null;
		String guid = null;
		String tacheId = null;
		String curUserName = null;
		
		final HttpSession httpSession = request.getSession(false);
		VUserInfoEntity userEntity = httpSession != null ? (VUserInfoEntity) httpSession.getAttribute(s_user) : null;
		// 当前用户姓名
		if (userEntity != null) {
			curUserName = userEntity.getUserName();
		}
		if (args != null && args.containsKey("entity") && args.get("entity") != null) {
			entityJson = args.get("entity").toString();
		}
		if (args.containsKey("guid") && args.get("guid") != null) {
			guid = args.get("guid").toString();
		}
		if (args.containsKey("name") && args.get("name") != null) {
			name = args.get("name").toString();
		}
		if (args.containsKey("tacheId") && args.get("tacheId") != null) {
			tacheId = args.get("tacheId").toString();
		}
		
		Map entity = JsonUtils.toBean(entityJson, Map.class);
		
		CheckInfo info = new CheckInfo();
		info.setDataid(guid);
		info.setName(name);
		info.setTacheid(tacheId);
		info.setUserName(curUserName);
		
		return checkService.check(entity,info);
	}

	/**
	 * 解决请求参数问题
	 * 
	 * @param data
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static HashMap<String, Object> getRestParam(String data) {
		HashMap<String, Object> args = null;
		if (data == null || "".equals(data.trim()))
			return args;
		// 解决不同浏览器返回传递参数不一致问题-s-
		try {
			args = JsonUtils.toBean(data, HashMap.class);

		} catch (Exception e) {
			try {
				String ENCODE = "UTF-8";
				data = java.net.URLDecoder.decode(data, ENCODE);
				if (data != null && !"".equals(data.trim())) {
					String[] lst = data.split("&");
					args = new HashMap<String, Object>();
					for (String item : lst) {
						if (item == null || "".equals(item.trim()))
							continue;
						String[] ivalue = item.split("=");
						if (ivalue != null) {
							if (ivalue.length > 1) {
								args.put(ivalue[0], ivalue[1]);
							} else {
								args.put(ivalue[0], null);
							}
						}

					}
				}
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
		}
		// 解决不同浏览器返回传递参数不一致问题-e-
		return args;
	}

}
